import React from "react";
import _ from 'lodash';
import { Form,Input, InputNumber, Button, } from "antd";
import GoBack from "@/components/TheGoBack";
import "../../style/style.less";
import TheTable from "../../../../components/TheTable";
import { exportSheetTable } from '@/utils/exportTable.js'

class Add extends React.Component {
  state = {
    btnLoading: false,
    total : 0,
    columns : [
      {
        title:"链接地址",
        dataIndex:"antifakeCode",
        key:"antifakeCode"
      }
    ],
    dataSource:[],
    isdata:false,
    loading:false,
    buttonloading:false,
  };

  goback = () => {
    this.props.history.go(-1);
  };


  getRangeCode = (len) =>{
    var  orgStr='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    // var  orgStr='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    let returnStr="";
    for (var i = 0; i < len; i++) {
      returnStr+=orgStr.charAt(Math.floor((Math.random()*orgStr.length)));
    }
    return returnStr
  }

  onSave = async (values) => {
    this.setState({ btnLoading: true })
    let Url = "https://www.hqmpet.com/share/#/pages/antiFake/index?short_url="
    let params = {
      batchNo:values.batchNo,
      antifakeNum:values.antifakeNum
    }
    let res = await React.$post("/management/tAntifakeCode/add",{...params})
    if(res.code === 0) {
      let data = []
      let tabledata = []
      for(let i = 0; i<res.data.length; i++){
        data.push(`${Url}${res.data[i]}`)
        tabledata.push({antifakeCode:`${Url}${res.data[i]}`})
      }
      React.$message({
        type: 'info',
        content: '生成链接地址成功'
      })
      this.setState({
        dataSource:data,
        TabledataSource:tabledata,
        isdata:true,
        btnLoading: false
      })
    }else{
      this.setState({
        btnLoading: false
      })
    }
  };

  export = (isdata)=>{
    this.setState({buttonloading:true})
    if(isdata){
      let th_arr = [{ prop: 'antifakeCode', title: '链接地址' },]
      let data = this.state.TabledataSource
      exportSheetTable({ fileName: '防伪链接', tH_data: th_arr, tB_data: data, isSheet: true, sheetSize: 50000, merge: false, mergeProp: false })
    }else{
      React.$message({
        type: 'warning',
        content: '请先生成链接地址'
      })
    }
    setTimeout(() => {
      this.setState({buttonloading:false})
    }, 5000);

  }

  render() {
    const { btnLoading, columns, TabledataSource, loading,buttonloading,isdata } = this.state;
    return (
      <div className="p-layout-box commodity-list-add">
        <GoBack history={this.props.history} title={"新增防伪链接"} />
        <div className="content">
          <div className="title">生成防伪链接</div>
          <div className="form-box">
            {/* <p>如需一次生成多张二维码，请耐心等待，默认一次生成一张二维码</p> */}
            <Form ref={ref => this.formRef = ref} onFinish={this.onSave} >
              <Form.Item label="批次" name={'batchNo'} rules={[{ required: true }]}>
                <InputNumber placeholder="请输入批次  规则：年-月-日-批次" maxLength={8} />

              </Form.Item>
              <Form.Item label="生成数量" name={'antifakeNum'} rules={[{ required: true }]}>
                <InputNumber max={10000} placeholder="请输入生成数量,最大10000"  />
              </Form.Item>
              <Form.Item>
                <Button type="default" size="middle" onClick={this.goback}>
                  取消
                </Button>
                <Button
                  style={{ backgroundColor: "#F59A23", color: "#fff" }}
                  size="middle"
                  loading={btnLoading}
                  htmlType="submit"
                >
                  生成
                </Button>
              </Form.Item>
            </Form>
          </div>
          <div className="title">新增防伪链接</div>
          <div className="button" style={{marginBottom:"10px",textAlign:"right"}} ><Button onClick={()=>this.export(isdata)} loading = {buttonloading} >导出</Button></div>
          <div>
            <TheTable columns = {columns} dataSource = {TabledataSource} loading={loading} pagination = {true} />

          </div>
        </div>
      </div>
    );
  }
}

export default Add;
